﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;

namespace TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainList
{
    public partial class uc_Locomotive_MainBase_TrainList_TrainNumber : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID, _LoadName_LoadTCID, _LoadTTID, _LoadRSID, _LoadRBID = "";
        public uc_Locomotive_MainBase_TrainList_TrainNumber()
        {
            InitializeComponent();
        }

        private void uc_Locomotive_MainBase_TrainList_TrainNumber_Load(object sender, EventArgs e)
        {
            TS_Win_Class.FillListTypeAct(cb_TrainClass, "select f_id,f_name from TS_S_T_P_TrainClass where f_del=0");
            TS_Win_Class.FillListTypeAct(cb_RB, "select f_id,f_name from TS_S_T_P_RailwayBureau where f_del=0");
            if (cb_TrainClass.SelectedIndex > -1)
            {
                TS_Win_Class.FillListTypeAct(cb_TrainType_List, "select f_id,f_name from TS_S_T_P_TrainType where f_del=0 and f_tcid=" + cb_TrainClass.SelectedValue.ToString());
            }
            if (cb_RB.SelectedIndex > -1)
            {
                TS_Win_Class.FillListTypeAct(cb_RS, "select f_id,f_name from TS_S_T_P_RailwayStations where f_del=0 and f_upid=" + cb_RB.SelectedValue.ToString());
            }
        }
        private void _LoadGrid()
        {
            string strSQL = "select row_number() over (order by f_name) as f_row,f_name,f_id,f_ttid,f_arbid,f_arsid,(select f_name from TS_S_T_P_RailwayBureau where f_id=TS_S_T_P_Train_List.f_arbid)as f_rbname,(select f_name from TS_S_T_P_RailwayStations where f_id=TS_S_T_P_Train_List.f_arsid)as f_rsname from TS_S_T_P_Train_List  where f_del=0 and f_ttid=" + cb_TrainType_List.SelectedValue.ToString();
            DataTable _dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, _dt);
            gc_Main.DataSource = _dt;
        }
        private void bt_Add_Click(object sender, EventArgs e)
        {
            string strSQL = "insert into TS_S_T_P_Train_List (f_name,f_ttid,f_ttname,f_arbid,f_arsid,f_workerid) values('"+tb_Name.Text.Trim().PadLeft(4,'0')+"',"+cb_TrainType_List.SelectedValue.ToString()+",'"+cb_TrainType_List.Text.Trim()+"',"+cb_RB.SelectedValue.ToString()+","+cb_RS.SelectedValue.ToString()+","+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+")";
            TS_Win_Class.MySqlCom(strSQL);
            _LoadGrid();
            tb_Name.Text = "";
        }

        private void bt_Edit_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_S_T_P_Train_List set f_name='" + tb_Name.Text.Trim().PadLeft(4, '0') + "',f_ttid=" + cb_TrainType_List.SelectedValue.ToString() + ",f_ttname='" + cb_TrainType_List.Text.Trim() + "',f_arbid=" + cb_RB.SelectedValue.ToString() + ",f_arsid=" + cb_RS.SelectedValue.ToString() + ",f_workerid="+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where f_id="+gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString();
            TS_Win_Class.MySqlCom(strSQL);
            _LoadGrid();
            tb_Name.Text = "";
        }

        private void bt_Del_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_S_T_P_Train_List set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString();
            TS_Win_Class.MySqlCom(strSQL);
            _LoadGrid();
            tb_Name.Text = "";
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gc_Main_Click(object sender, EventArgs e)
        {
            if (gv_Main.SelectedRowsCount > 0)
            {
                tb_Name.Text= gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_name").ToString();
                cb_TrainType_List.SelectedValue = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_ttid").ToString();
                cb_RB.SelectedValue = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_arbid").ToString();
                cb_RS.SelectedValue = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_arsid").ToString();
            }
            else
            {
                tb_Name.Text = "";
            }
        }

        private void cb_TrainClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_TrainClass.SelectedIndex > -1)
            {
                TS_Win_Class.FillListTypeAct(cb_TrainType_List, "select f_id,f_name from TS_S_T_P_TrainType where f_del=0 and f_tcid=" + cb_TrainClass.SelectedValue.ToString());
            }
        }

        private void cb_TrainType_List_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_TrainType_List.SelectedIndex > -1)
            {
                _LoadGrid();
            }
            else
            {
                gc_Main.DataSource = null;
                gc_Main.Refresh();
            }
            _SetButtonEnabled();
        }

        private void cb_RB_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_RB.SelectedIndex > -1)
            {
                TS_Win_Class.FillListTypeAct(cb_RS, "select f_id,f_name from TS_S_T_P_RailwayStations where f_del=0 and f_upid=" + cb_RB.SelectedValue.ToString());
            }
        }

        private void cb_RS_SelectedIndexChanged(object sender, EventArgs e)
        {
            _SetButtonEnabled();
        }

        private void tb_Name_TextChanged(object sender, EventArgs e)
        {
            _SetButtonEnabled();
        }
        private void _SetButtonEnabled()
        {
            if (tb_Name.Text.Trim().Length > 0 && cb_TrainType_List.SelectedIndex > -1 && cb_RS.SelectedIndex > -1)
            {
                bt_Add.Enabled = true;
                if (gv_Main.SelectedRowsCount > 0)
                {
                    bt_Edit.Enabled = true;
                    bt_Del.Enabled = true;
                }
                else
                {
                    bt_Edit.Enabled = false;
                    bt_Del.Enabled = false;
                }
            }
            else
            {
                bt_Add.Enabled = false;
                bt_Edit.Enabled = false;
                bt_Del.Enabled = false;
            }
        }
    }
}